package article

import (
	"strconv"

	"10apiserver/common/error10"
	"10apiserver/common/util"
	AM "10apiserver/models/article"

	"github.com/gin-gonic/gin"
	"github.com/lexkong/log"
	"github.com/lexkong/log/lager"
)

func Create(c *gin.Context) {
	log.Info("Article Create function called.", lager.Data{"X-Request-Id": util.GetReqID(c)})
	var art AM.ArticleInfo
	var err error
	if err = c.Bind(&art); err != nil {
		util.SendResponse(c, error10.New(error10.ErrBind, err), nil)
		return
	}

	if err = art.Create(); err != nil {
		util.SendResponse(c, error10.New(error10.ErrDatabase, err), nil)
		return
	}

	util.SendResponse(c, nil, art)
}

func Update(c *gin.Context) {
	log.Info("Article Update function called.", lager.Data{"X-Request-Id": util.GetReqID(c)})
	var art AM.ArticleInfo
	var err error
	if err = c.Bind(&art); err != nil {
		util.SendResponse(c, error10.New(error10.ErrBind, err), nil)
		return
	}

	if err = art.Update(); err != nil {
		util.SendResponse(c, error10.New(error10.ErrDatabase, err), nil)
		return
	}

	util.SendResponse(c, nil, art)
}

func Delete(c *gin.Context) {
	log.Info("Article Delete function called.", lager.Data{"X-Request-Id": util.GetReqID(c)})

	artID, _ := strconv.ParseInt(c.Param("id"), 10, 64)
	art := AM.ArticleInfo{}
	art.ID = artID
	if err := art.Delete(); err != nil {
		util.SendResponse(c, error10.New(error10.ErrDatabase, err), nil)
		return
	}

	util.SendResponse(c, nil, nil)

}

func IncReadCount(c *gin.Context) {
	log.Info("Article IncReadCount function called.", lager.Data{"X-Request-Id": util.GetReqID(c)})

	filePath := c.Param("path")

	artInfo, err := AM.GetByPath(filePath)
	if err != nil {
		util.SendResponse(c, error10.New(error10.ErrArticleNotFound, err), nil)
		return
	}

	if err := artInfo.IncReadCount(); err != nil {
		util.SendResponse(c, error10.New(error10.ErrDatabase, err), nil)
		return
	}

	util.SendResponse(c, nil, nil)

}

func DeleteByPath(c *gin.Context) {
	log.Info("Article Delete by file path function called.", lager.Data{"X-Request-Id": util.GetReqID(c)})

	art := AM.ArticleInfo{}
	art.FilePath = c.Param("path")
	if err := art.DeleteByPath(); err != nil {
		util.SendResponse(c, error10.New(error10.ErrDatabase, err), nil)
		return
	}

	util.SendResponse(c, nil, nil)

}

func GetByID(c *gin.Context) {
	log.Info("Article LoadSingle function called.", lager.Data{"X-Request-Id": util.GetReqID(c)})

	artID, _ := strconv.ParseInt(c.Param("id"), 10, 64)

	artInfo, err := AM.GetByID(artID)
	if err != nil {
		util.SendResponse(c, error10.New(error10.ErrArticleNotFound, err), nil)
		return
	}

	util.SendResponse(c, nil, artInfo)
}

func GetByPath(c *gin.Context) {
	log.Info("Article LoadSingle function called.", lager.Data{"X-Request-Id": util.GetReqID(c)})

	filePath := c.Param("path")

	artInfo, err := AM.GetByPath(filePath)
	if err != nil {
		util.SendResponse(c, error10.New(error10.ErrArticleNotFound, err), nil)
		return
	}

	util.SendResponse(c, nil, artInfo)
}

func List(c *gin.Context) {
	var ar AM.ArticlesRequest
	ar.Tag = c.Query("tag")
	ar.Offset, _ = strconv.Atoi(c.Query("offset"))
	ar.Limit, _ = strconv.Atoi(c.Query("limit"))
	// if err := c.Bind(&ar); err != nil {
	// 	util.SendResponse(c, error10.New(error10.ErrBind, err), nil)
	// 	return
	// }

	artsResult, err := AM.ListArticles(ar.Tag, ar.Offset, ar.Limit)
	if err != nil {
		util.SendResponse(c, error10.New(error10.ErrDatabase, err), nil)
		return
	}

	util.SendResponse(c, nil, artsResult)
}
